package com.qianfeng.openapi.web.master.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qianfeng.openapi.web.master.feign.CacheFeign;
import com.qianfeng.openapi.web.master.mapper.ApiMappingMapper;
import com.qianfeng.openapi.web.master.pojo.ApiMapping;
import com.qianfeng.openapi.web.master.service.ApiMappingService;
import com.qianfeng.openplatform.common.SystemParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author LJF
 * @Date 2020/7/2 15:37
 * @Version 1.0
 */
@Service
@Transactional
public class ApiMappingServiceImpl implements ApiMappingService {

    @Autowired
    private CacheFeign cacheFeign;

    @Autowired
    private ApiMappingMapper apiMappingMapper;

    @Override
    public int insertApiMapping(ApiMapping apiMapping) {
        int i = apiMappingMapper.insertApiMapping(apiMapping);

        if (apiMapping.getState() == 1) {
            try {
                cacheFeign.hMset(SystemParams.METHOD_REDIS_PRE + apiMapping.getGatewayApiName(), apiMapping);
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

        return i;
    }

    @Override
    public int updateApiMapping(ApiMapping apiMapping) {
        int i = apiMappingMapper.updateApiMapping(apiMapping);
        try {
            if (apiMapping.getState()==1){
                cacheFeign.hMset(SystemParams.METHOD_REDIS_PRE+apiMapping.getGatewayApiName(),apiMapping);
            }else {
                cacheFeign.deleteKey(SystemParams.METHOD_REDIS_PRE+apiMapping.getGatewayApiName());
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int deleteApiMapping(Integer id) {
        return apiMappingMapper.deleteApiMapping(id);
    }

    @Override
    public ApiMapping getApiMappingById(Integer id) {
        return apiMappingMapper.getApiMappingById(id);
    }

    @Override
    public void deleteApiMappings(Integer[] ids) {
        if (ids!=null || ids.length==0) {
            return;
        }
        for (Integer id : ids) {
            ApiMapping apiMappingById = apiMappingMapper.getApiMappingById(id);
            int i = apiMappingMapper.deleteApiMapping(id);
            if (i==1 && apiMappingById!=null){
                try {
                    cacheFeign.deleteKey(SystemParams.METHOD_REDIS_PRE+apiMappingById.getGatewayApiName());
                } catch (Exception exception) {
                    exception.printStackTrace();
                }
            }
        }
    }

    @Override
    public PageInfo<ApiMapping> getAllApiMappingByPage(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<ApiMapping> apiMappingList = apiMappingMapper.getAllApiMappings();
        return new PageInfo<>(apiMappingList);
    }


    @Override
    public PageInfo<ApiMapping> selectApiMappingByCondition(Integer page, Integer limit, String gatewayApiName, Integer state) {
        PageHelper.startPage(page, limit);
        List<ApiMapping> apiMappingList = apiMappingMapper.selectApiMappingByCondition(gatewayApiName, state);
        return new PageInfo<>(apiMappingList);
    }
}
